/*
 * Created by JFormDesigner on Mon Jun 21 16:21:07 GMT+08:00 2021
 */

package com.bk.view;

import java.awt.event.*;

import com.bk.dao.BookDao;
import com.bk.dao.BookTypeDao;
import com.bk.model.Book;
import com.bk.util.ToolUtil;

import java.awt.*;
import java.sql.SQLException;
import java.util.List;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Ayi
 */
// 用户编辑窗口
public class bEditFrame extends JFrame {
    Book book;
    public bEditFrame(Book book) {
        initComponents();
        this.book = book;
        try {
            set();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    // 初始化当前User的信息到窗口
    public void set() throws SQLException, ClassNotFoundException {
        textField1.setText(book.getBname().substring(1, book.getBname().length() - 1));
        spinner1.setValue(book.getBinventory());
        List<String> booktype = new BookTypeDao().getAllBookType();
        for (String x : booktype) {
            comboBox1.addItem(x);
        }
        comboBox1.setSelectedItem(book.getBtype());
    }

    // 编辑，监听
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String bname = textField1.getText();
        int binventory = (int)spinner1.getValue();
        String btype = comboBox1.getSelectedItem().toString();
        try {
            if (new BookDao().editBook(book, bname, binventory, btype)) {
                JOptionPane.showMessageDialog(null, "编辑成功");
                dispose();
            }else{
                JOptionPane.showMessageDialog(null, "编辑失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    private void textField1KeyTyped(KeyEvent e) {
        // TODO add your code here
        // 文本框监听，限制输入长度
        String s = textField1.getText();
        if (s.length() > 7) {
            e.consume();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - unknown
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        spinner1 = new JSpinner();
        comboBox1 = new JComboBox();
        label3 = new JLabel();
        button1 = new JButton();

        //======== this ========
        setResizable(false);
        setTitle("\u67e5\u627e\u4e0e\u7f16\u8f91");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u4e66\u540d");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 2f));

        //---- textField1 ----
        textField1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                textField1KeyTyped(e);
            }
        });

        //---- label2 ----
        label2.setText("\u4f59\u91cf");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 2f));

        //---- spinner1 ----
        spinner1.setBackground(Color.white);
        spinner1.setModel(new SpinnerNumberModel(0, 0, null, 1));

        //---- label3 ----
        label3.setText("\u7c7b\u578b");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 2f));

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u7f16\u8f91");
        button1.setBackground(Color.white);
        button1.addActionListener(e -> button1ActionPerformed(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(60, 60, 60)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(button1, GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label1)
                                .addComponent(label2)
                                .addComponent(label3))
                            .addGap(30, 30, 30)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(textField1, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                                .addComponent(spinner1)
                                .addComponent(comboBox1, GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))))
                    .addGap(59, 59, 59))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(38, 38, 38)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(26, 26, 26)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(spinner1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label2))
                    .addGap(27, 27, 27)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label3))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                    .addComponent(button1)
                    .addGap(27, 27, 27))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - unknown
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JSpinner spinner1;
    private JComboBox comboBox1;
    private JLabel label3;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
